import { getcookie } from '@/utils/cookie'
import { RouteConfig } from "vue-router"
import { allRouters } from "./allRouter"

// 筛选出符合权限的路由
var asyncRouters :RouteConfig[] = []

// 递归查询当前路由是否含有子路由
const hasPermission = (roles:string[],router:RouteConfig) =>{
    if(router.meta && router.meta.roles){
        return roles.some(role => router.meta.roles.includes(role))
    }else{
        return true
    }
}

// 根据权限身份查询路由
const asyncPermission = (roles:string[],routes:RouteConfig[]) =>{
    const res:RouteConfig[] = []
    routes.forEach(route=>{
        const r = route as any
        if(hasPermission(roles,r)){
            if(r.children){
                r.children = asyncPermission(roles,r.children)
            }
            res.push(r as RouteConfig)
        }
    })
    return res
}

const role = getcookie() as string;

const rolelist = [];

rolelist.push(role)

asyncRouters = asyncPermission(rolelist,allRouters)

// 导出筛选出来的路由
export default asyncRouters